package io.adrainty.bolee.trade.handler;

import io.adrainty.bolee.framework.commons.utils.EnumUtil;
import io.adrainty.bolee.trade.models.valobj.PayChannelEnum;
import org.springframework.stereotype.Component;

import java.util.EnumMap;
import java.util.List;
import java.util.Map;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/31 下午11:22
 */

@Component
public class PayHandlerRegistry {

    private final Map<PayChannelEnum, IPayHandler> payHandlerMap;

    public PayHandlerRegistry(List<IPayHandler> handlerList) {
        payHandlerMap = new EnumMap<>(PayChannelEnum.class);
        for (IPayHandler handler : handlerList) {
            payHandlerMap.put(handler.supportPayChannel(), handler);
        }
    }

    public IPayHandler strictGetPayChannel(PayChannelEnum payChannel) {
        if (payChannel == null) {
            throw new IllegalArgumentException("payChannel can not be null");
        }
        IPayHandler payHandler = payHandlerMap.get(payChannel);
        if (payHandler == null) {
            throw new UnsupportedOperationException("payChannel " + payChannel + " not supported");
        }
        return payHandler;
    }

    public IPayHandler strictGetPayChannel(String payChannel) {
        PayChannelEnum payChannelEnum = EnumUtil.getEnumConstants(payChannel, PayChannelEnum.class);
        return strictGetPayChannel(payChannelEnum);
    }

}
